<template>
  <div class="app-container">
    <div class="filter-container">
      <org-unit-select v-model="filters.orgUnitId" placeholder="请选择店铺" :org-unit-type="1" />
      <el-date-picker
        v-model="selectedDate"
        type="datetimerange"
        align="right"
        unlink-panels
        range-separator="至"
        value-format="yyyy-MM-dd HH:mm:ss"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        :default-time="['06:00:00','06:00:00']"
        :picker-options="pickerOptions">
      </el-date-picker>
      <el-radio-group v-model="filters.status" style="margin: 0 3px;" >
        <el-radio-button :label="1" @click.native.prevent="statusFilter(1)">拼车中</el-radio-button>
        <el-radio-button :label="2" @click.native.prevent="statusFilter(2)">已发车</el-radio-button>
        <el-radio-button :label="3" @click.native.prevent="statusFilter(3)">已结束</el-radio-button>
      </el-radio-group>
      <el-radio-group v-model="filters.confirmed" style="margin: 0 3px;" >
        <el-radio-button :label="0" @click.native.prevent="confirmedFilter(0)">未确认</el-radio-button>
        <el-radio-button :label="1" @click.native.prevent="confirmedFilter(1)">已确认</el-radio-button>
      </el-radio-group>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
      <!-- <el-button v-waves class="filter-item" type="primary" @click="handleExporter">导出</el-button> -->
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      border
      fit
      :height="tableHeight"
      :data="list"
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column prop="orgUnitName" label="门店" min-width="120" />
      <el-table-column prop="userNickname" label="客服昵称" width="260" />
      <el-table-column prop="userPhoneNumber" label="客服手机号" width="260" />
      <el-table-column prop="count" label="推客数量" width="120" />
    </el-table>
  </div>
</template>

<script>
import { playerCountReport } from '@/api/player'
import OrgUnitSelect from '../components/org-unit-select'
import waves from '@/directive/waves' // waves directive
import { mapGetters } from 'vuex'

export default {
  name: 'CarpoolPlayerCountReport',
  components: { OrgUnitSelect },
  directives: { waves },
  data() {
    return {
      tableHeight: 260,
      tableKey: 0,
      list: [],
      listLoading: true,
      sels: [], // 列表选中列
      filters: {
        orgUnitId: null,
        status: null,
        confirmed: null
      },
      selectedDate: null,
      defaultTime: ['06:00:00', '06:00:00'],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            end.setHours(6, 0, 0, 0)
            const start = new Date()
            start.setHours(6, 0, 0, 0)
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近30天',
          onClick(picker) {
            const end = new Date()
            end.setHours(6, 0, 0, 0)
            const start = new Date()
            start.setHours(6, 0, 0, 0)
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近90天',
          onClick(picker) {
            const end = new Date()
            end.setHours(6, 0, 0, 0)
            const start = new Date()
            start.setHours(6, 0, 0, 0)
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  computed: {
    ...mapGetters(['userInfo', 'formLabelWidth', 'dynamicColumn'])
  },
  created() {
    this.tableHeight = window.innerHeight - 160
    this.listLoading = false
  },
  methods: {
    loadPageList() {
      this.listLoading = true
      const param = { ...this.filters }
      if (this.selectedDate && this.selectedDate.length >= 2) {
        param.start = this.selectedDate[0]
        param.end = this.selectedDate[1]
      }
      playerCountReport(param).then(({ success, message, data }) => {
        this.listLoading = false
        if (success) {
          this.list = data.records
        } else {
          this.$message({
            message: message || '查询列表失败',
            type: 'error'
          })
        }
      }, () => {
        this.listLoading = false
      })
    },
    handleFilter() {
      this.loadPageList()
    },
    statusFilter(status) {
      if (status === this.filters.status) {
        this.filters.status = null
      } else {
        this.filters.status = status
      }
      this.handleFilter()
    },
    confirmedFilter(status) {
      if (status === this.filters.confirmed) {
        this.filters.confirmed = null
      } else {
        this.filters.confirmed = status
      }
      this.handleFilter()
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
